package com.zc.controller.oldStore;

import com.alibaba.fastjson.JSONObject;
import com.zc.dto.ResultDto;
import com.zc.utils.WebSocketSubject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/websocket")
@Controller
public class WebsocketController {
    @RequestMapping("/prePublish")
    public String prePublish(){
        return "websocket";
    }


    @ResponseBody
    @RequestMapping("/publish")
    public ResultDto publish(String message,String principal){
        ResultDto result = new ResultDto();
        String type = "radio";
        JSONObject data = new JSONObject();
        data.put("title", "test web socket");
        data.put("content",message);
        try {
            WebSocketSubject subject = WebSocketSubject.Holder.getSubject(principal);
            subject.notify(type, data.toJSONString());
            result.setSuccess(true);
            result.setMessage("success");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage(e+"");
        }
        return result;
    }

    @RequestMapping("/subscribe")
    public String subscribe(){
        return "testWebsocket";
    }

}
